<template>
    <div>
        <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="父级菜单" prop="parentId">
                <el-cascader :props="{ value: 'menuId',label:'menuName', checkStrictly: true }" v-model="value"
                    :options="options" @change="handleChange" style="width: 46%;"></el-cascader>
            </el-form-item>
            <el-form-item label="菜单名称" prop="menuName">
                <el-input v-model="ruleForm.menuName"></el-input>
            </el-form-item>
            <el-form-item label="菜单路由" prop="route">
                <el-input v-model="ruleForm.route"></el-input>
            </el-form-item>
            <el-form-item label="组件路径" prop="componentPath">
                <el-input v-model="ruleForm.componentPath"></el-input>
            </el-form-item>
            <el-form-item label="是否显示" prop="isShow">
                <el-switch v-model="ruleForm.isShow"></el-switch>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import axios from '../../util/request';
    export default {
        name: 'CodeUIAdminCreate',

        data() {
            return {
                ruleForm: {
                    parentId: 0,
                    menuName: '',
                    route: '',
                    componentPath: '',
                    isShow: false
                },
                options: [],
                value: []
            };
        },

        mounted() {
            axios.get('/api/menu').then(m => {
                var reg = new RegExp('\\,"children":\\[]', 'g')
                this.options = JSON.parse(JSON.stringify(m.data).replace(reg, ''))
            });
        },

        methods: {
            handleChange(value) {
                console.log(value);
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.ruleForm.parentId = this.value.slice(-1).toString()
                        axios.post('/menu/create', this.ruleForm).then(m => {
                            //1、弹出消息
                            if (m.data.code > 200) {
                                this.$message({
                                    message: m.data.message,
                                    type: 'warning'
                                });
                            }
                            else {
                                this.$message({
                                    message: '添加成功',
                                    type: 'success'
                                });
                                //2、关闭弹窗
                                this.$emit('OnSave')
                            }
                            //3、刷新列表
                        });
                    } else {
                        return false;
                    }
                });
            },
        },
    };
</script>

<style>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 88px;
        height: 88px;
        line-height: 88px;
        text-align: center;
    }

    .avatar {
        width: 88px;
        height: 88px;
        display: block;
    }
</style>